package com.nulldev.util.java.swing.extSwing;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import com.nulldev.util.java.swing.SwingUtil;
import com.nulldev.util.java.swing.extSwing.lookandfeel.themes.ThemeApplier;
import com.nulldev.util.java.swing.font.FontManager;
import com.nulldev.util.threading.legacy.ThreadExecutor;
import javax.swing.JCheckBox;
import javax.swing.JScrollPane;

public class eTOSDisplay extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JLabel lblExtswingemessagetitle;
	private JTextArea txtrExtswingmessage;
	private boolean pressed;
	private boolean accepted;
	private Runnable r = new Runnable() {

		@Override
		public void run() {
			while (true) {
				if (pressed) {
					setAlwaysOnTop(false);
					setVisible(false);
					dispose();
					break;
				}
				try {
					Thread.sleep(SwingUtil.FPS_30);
				} catch (InterruptedException e) {
				}
			}
		}
	};

	/**
	 * Launch the application.
	 * 
	 * @return
	 */
	public static eTOSDisplay main() {
		final eTOSDisplay dialog = new eTOSDisplay();
		ThemeApplier.applyTheme(dialog);
		dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		dialog.setVisible(true);
		dialog.setLocation(
				(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode().getWidth()
						- dialog.getWidth()) / 2,
				(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode().getHeight()
						- dialog.getHeight()) / 2);
		return dialog;
	}

	/**
	 * Create the dialog.
	 */
	public eTOSDisplay() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				pressed = true;
			}
		});
		if (ThemeApplier.useSystem) {
			try {
				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			} catch (Exception e) {
			}
		}

		setBounds(100, 100, 543, 451);
		getContentPane().setLayout(new BorderLayout());
		setTitle("extSwing.eMessage");
		setResizable(false);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);

		JButton btnOk = new JButton("OK");
		btnOk.setFocusable(false);
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pressed = true;
			}
		});

		lblExtswingemessagetitle = new JLabel("extSwing.eMessage.title");
		
		final JCheckBox chckbxIAcceptThe = new JCheckBox("I accept the terms and conditions provided");
		chckbxIAcceptThe.setFocusable(false);
		chckbxIAcceptThe.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				accepted = chckbxIAcceptThe.isSelected();
			}
		});
		
		JScrollPane scrollPane = new JScrollPane();
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(chckbxIAcceptThe)
						.addComponent(btnOk, Alignment.TRAILING)
						.addComponent(lblExtswingemessagetitle)
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 509, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblExtswingemessagetitle)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 295, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(chckbxIAcceptThe)
					.addPreferredGap(ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
					.addComponent(btnOk)
					.addContainerGap())
		);
		
				txtrExtswingmessage = new JTextArea() {
					
					@Override
					public void setFont(final Font font) {
						super.setFont(FontManager.getTraffic(16));
					}
					
				};
				scrollPane.setViewportView(txtrExtswingmessage);
				txtrExtswingmessage.setText("extSwing.message");
				txtrExtswingmessage.setEditable(false);
				txtrExtswingmessage.setFocusable(false);
		contentPanel.setLayout(gl_contentPanel);
	}

	public void setMessageTitle(final String title) {
		this.lblExtswingemessagetitle.setText(title);
	}

	public void setWindowTitle(final String title) {
		this.setTitle(title);
	}

//	public void setMessage(final String message) {
//		this.txtrExtswingmessage.setText(message);
//	}

	public void setMessage(final String message) {
		final int len = message.length() * this.txtrExtswingmessage.getFont().getSize();
//		if (len > this.txtrExtswingmessage.getWidth()) {
//			final String[] xy = StringsUtil.splitEquallyA(message, 48);
//			// final String z = StringsUtil.toStringNL(xy);
//			this.txtrExtswingmessage.setText(message);
//			this.setSize(384 + (message.length() * 3),
//					159 + (xy.length * this.txtrExtswingmessage.getFont().getSize()));
//			SwingUtil.centerFrame(this);
//			setLocation(
//					(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode()
//							.getWidth() - getWidth()) / 2,
//					(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode()
//							.getHeight() - getHeight()) / 2);
//		} else {
//			this.txtrExtswingmessage.setText(message);
//		}
		this.txtrExtswingmessage.setText(message);
	}

	public boolean waitForOK() {
		setAlwaysOnTop(true);
		ThreadExecutor.execute(r);
		while (true) {
			if (this.pressed) {
				break;
			}
			if (!this.isVisible()) {
				break;
			}
			try {
				Thread.sleep(50L);
			} catch (InterruptedException e) {
			}
		}
		return this.accepted;
	}
	
	public boolean accepted() {
		return this.accepted;
	}
}
